package com.msmall.base.rms.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import com.msmall.base.mapper.SqlMapper;
import com.msmall.base.rms.po.Group;
import com.msmall.base.rms.po.GroupModule;
import com.msmall.base.rms.po.Module;


@Repository("moduleMapper")
public interface ModuleMapper extends SqlMapper{

	@Insert("INSERT INTO t_rms_module(module_name,remark,url, parent_id, type) "
			+ "VALUES(#{module.moduleName},#{module.remark},#{module.url},#{module.parentid},#{module.type})")
	@SelectKey(before = false, keyProperty = "module.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public void insertModule(final @Param("module") Module module);

	@Delete("DELETE FROM t_rms_module WHERE id=#{moduleid}")
	public void deleteModule(@Param("moduleid") final Integer moduleid);

	@Delete("DELETE FROM t_rms_group_module WHERE group_id=#{groupid}")
	public void deleteGroupModuleByGroup(@Param("groupid") final Integer groupid);
	
	@Delete("DELETE FROM t_rms_group_module WHERE module_id=#{moduleid}")
	public void deleteGroupModuleByModule(@Param("moduleid") final Integer moduleid);
	
	@Update("UPDATE t_rms_module SET module_name=#{module.moduleName}, remark=#{module.remark}, url=#{module.url}, parent_id=#{module.parentid}, type=#{module.type} WHERE id=#{module.id}")
	public void updateModule(@Param("module") final Module module);
	
	public Module selectModuleByid(final @Param("id") Integer id);
	
//	public List<Module> selectModuleByGroup(final @Param("groupid") Integer groupid);
	
	@Insert("INSERT INTO t_rms_group_module(group_id,module_id) "
			+ "VALUES(#{gm.groupid},#{gm.moduleid})")
	@SelectKey(before = false, keyProperty = "gm.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public void insertGroupModule(final @Param("gm") GroupModule gm);
	
	public List<Module> selectModuleByAdministerAndUrl(final @Param("administerid") Integer administerid, final @Param("url") String url);
	
	public List<Module> selectModuleByAdminister(final @Param("administerid") Integer administerid);
	
	public Integer findModulePageCount(@Param("moduleName") String moduleName);
	
	public List<Map<String, Object>> findModulePage(@Param("moduleName") String moduleName, @Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);

	
	/**
	 * 获得模块的父模块列表
	 */
	public List<Module> getParentModule(@Param("moduleid") Integer moduleid) throws Exception;
	/**
	 * 获得模块的子模块列表
	 */
	public List<Module> getChildrenModule(@Param("moduleid") Integer moduleid) throws Exception;
	
	/**
	 *  选出所有模块，并且选出被授权给groupid组的关系表ID
	 * @param groupid
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> getAuthModuleByAGroupJson(final @Param("groupid") Integer groupid);
	
	public Integer findGroupPageCount(@Param("groupName") String groupName);
	
	public List<Group> findGroupPage(@Param("groupName") String groupName, @Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);
	
	@Insert("INSERT INTO t_rms_group(group_name,remark) "
			+ "VALUES(#{group.groupName},#{group.remark})")
	@SelectKey(before = false, keyProperty = "group.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public void insertGroup(final @Param("group") Group group);
	
	@Update("UPDATE t_rms_group SET group_name=#{group.groupName}, remark=#{group.remark} WHERE id=#{group.id}")
	public void updateGroup(final @Param("group") Group group);
	
	@Delete("DELETE FROM t_rms_group WHERE id=#{id}")
	public void deleteGroup(@Param("id") final Integer id);
	
	@Delete("DELETE FROM t_rms_administer_group WHERE group_id=#{groupid}")
	public void deleteAdministerGroupByGroup(@Param("groupid") final Integer groupid);
	
	public Group selectGroupByid(@Param("id") Integer id);
	
	public List<Group> selectAllGroup();
	
	public List<Group> selectGroupByAdminister(@Param("administerid") Integer administerid);

}
